#include <iostream>
#include <ThreadPool.h>
#include <chrono>

class TestPool
{
public:
    void test_print(int i)
    {
        std::cout << __func__ << " " << i << std::endl;
    }
};
int gTestNum = 10000;

void pool_test(int i)
{ 
    std::cout << __func__ << " " << i << std::endl;
}

void test_1_func(int i)
{
    int a=0;
    for(int i=0; i<gTestNum; i++) {
        a++;
    }
}

void test_1()
{
    WTP::ThreadPool pool(1, 1, 2);
    int num = 0;
    auto start = std::chrono::high_resolution_clock::now();
    for(int i=0; i<10; i++)
        pool.runTask(test_1_func, num);

    pool.stop(false);
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "test 1 run time:" << std::chrono::duration<double, std::nano>(end - start).count() << " ns" << std::endl;
}

std::mutex gMutex;
void test_2_func(int i)
{
    std::unique_lock<std::mutex> lock(gMutex);
    int a=0;
    for(int i=0; i<gTestNum; i++) {
        a++;
    }
}

void test_2()
{
    WTP::ThreadPool pool(2, 2, 2);
    int num = 0;
    auto start = std::chrono::high_resolution_clock::now();
    for(int i=0; i<10; i++)
        pool.runTask(test_2_func, num);

    pool.stop(false);
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "test 2 run time:" << std::chrono::duration<double, std::nano>(end - start).count() << " ns" << std::endl;
}

int main(int, char**) {
    #if 0
    std::cout << "Hello, world!\n";

    WTP::ThreadPool pool;
    
    TestPool test;

    std::cout << "start\n";
    pool.start();
    for(int i=0; i<10; i++) {
        pool.runTask(pool_test, i);
        pool.runTaskWithObj(&test, TestPool::test_print, i);
    }

    std::cout << "stop\n";
    pool.stop(false);
    #else
    test_1();
    test_2();
    #endif
}
